package be.quadreg.dojo.persistence;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import be.quadreg.dojo.model.entity.AddressMemberAttribute;
import be.quadreg.dojo.model.entity.BeltHistoryMemberAttribute;
import be.quadreg.dojo.model.entity.BeltHistoryMemberAttribute.BeltHistory;
import be.quadreg.dojo.model.entity.EmailMemberAttribute;
import be.quadreg.dojo.model.entity.Member;
import be.quadreg.dojo.model.entity.MemberAttribute;
import be.quadreg.dojo.model.entity.MemberSubscription;
import be.quadreg.dojo.model.entity.SubscriptionHistoryMemberAttribute;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.util.DAOBase;

public class GenericDAO extends DAOBase {
	static {
		ObjectifyService.register(Member.class);
		ObjectifyService.register(MemberAttribute.class);
		ObjectifyService.register(AddressMemberAttribute.class);
		ObjectifyService.register(BeltHistoryMemberAttribute.class);
		ObjectifyService.register(BeltHistory.class);

		ObjectifyService.register(EmailMemberAttribute.class);
		ObjectifyService.register(SubscriptionHistoryMemberAttribute.class);
		ObjectifyService.register(MemberSubscription.class);
	}

	private static GenericDAO instance;

	public static synchronized GenericDAO getInstance() {
		if (instance == null) {
			instance = new GenericDAO();
		}

		return instance;
	}

	private GenericDAO() {
	}

	public Key<Member> persistMember(Member entity) {
		System.out.println("Persisting member: " + entity);
		return ofy().put(entity);
	}

	public Key<MemberSubscription> persistMemberSubscription(
			MemberSubscription entity) {
		System.out.println("Persisting memberSubscriptions: " + entity);
		return ofy().put(entity);
	}

	public Key<MemberAttribute> persistMemberAttribute(MemberAttribute entity) {
		System.out.println("Persisting memberAttribute: " + entity);
		return ofy().put(entity);
	}

	public Key<BeltHistory> persistBeltHistory(BeltHistory entity) {
		System.out.println("Persisting BeltHistory: " + entity);
		return ofy().put(entity);
	}

	public void removeMemeber(Member member) {
		ofy().delete(member);
	}

	public Collection<Member> getAllMembers() {
		System.out.println("GetAllMembers");
		// Map<Key<Member>, Member> result = ofy().get(Member.class);

		List<Member> result = ofy().query(Member.class).list();
		System.out.println("\tresult: " + result);
		for (Member m : result) {
			System.out.println("MemberAttributes:");
			for (MemberAttribute ma : m.getMemberAttributes()) {
				System.out.println("\t" + ma + ", ");
			}
		}

		return result;

	}

	public Collection<MemberAttribute> getMemberAttributes(
			Set<Key<MemberAttribute>> memberAttributeKeys) {
		return ofy().get(memberAttributeKeys).values();
	}

	public Collection<MemberSubscription> getSubscriptionHistory(
			Set<Key<MemberSubscription>> subscriptionHistoryKeys) {
		return ofy().get(subscriptionHistoryKeys).values();

	}

	public Collection<BeltHistory> getBeltHistory(
			Set<Key<BeltHistory>> beltHistoryKeys) {
		return ofy().get(beltHistoryKeys).values();
	}

}
//